package com.cleaningcloud.erp.controller;

import com.cleaningcloud.common.core.utils.poi.ExcelUtil;
import com.cleaningcloud.common.core.web.controller.BaseController;
import com.cleaningcloud.common.core.web.domain.AjaxResult;
import com.cleaningcloud.common.core.web.page.TableDataInfo;
import com.cleaningcloud.common.log.annotation.Log;
import com.cleaningcloud.common.log.enums.BusinessType;
import com.cleaningcloud.common.security.annotation.RequiresPermissions;
import com.cleaningcloud.erp.domain.ErpWarehousing;
import com.cleaningcloud.erp.domain.dto.ErpWarehousingDto;
import com.cleaningcloud.erp.domain.excelExport.WarehousingExcelExport;
import com.cleaningcloud.erp.domain.vo.ErpWarehousingDetailsVo;
import com.cleaningcloud.erp.domain.vo.ErpWarehousingVo;
import com.cleaningcloud.erp.service.IErpWarehousingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 采购入库Controller
 *
 * @author cleaningcloud
 * @date 2024-09-29
 */
@RestController
@RequestMapping("/ErpWarehousing")
public class ErpWarehousingController extends BaseController {
    @Autowired
    private IErpWarehousingService erpWarehousingService;

    /**
     * 查询采购入库列表
     */
    @RequiresPermissions("erp:ErpWarehousing:list")
    @GetMapping("/list")
    public TableDataInfo list(ErpWarehousingDto erpWarehousing) {
        startPage();
        List<ErpWarehousingVo> list = erpWarehousingService.selectErpWarehousingList(erpWarehousing);
        return getDataTable(list);
    }

    /**
     * 导出采购入库列表
     */
    @RequiresPermissions("erp:ErpWarehousing:export")
    @Log(title = "采购入库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ErpWarehousingDto erpWarehousing) {
        List<ErpWarehousingVo> list = erpWarehousingService.selectErpWarehousingList(erpWarehousing);
        ExcelUtil<ErpWarehousingVo> util = new ExcelUtil<>(ErpWarehousingVo.class);
        util.exportExcel(response, list, "采购入库数据");
    }

    /**
     * 导出采购入库详细列表
     */
    @RequiresPermissions("erp:ErpWarehousing:export")
    @Log(title = "采购入库", businessType = BusinessType.EXPORT)
    @PostMapping("/exportDetails/{warehousingId}")
    public void exportDetails(HttpServletResponse response, @PathVariable("warehousingId") Long warehousingId) {
        ErpWarehousingVo erpWarehousingVo = erpWarehousingService.selectErpWarehousingByWarehousingId(warehousingId);
        List<WarehousingExcelExport> list = new ArrayList<>();
        for (ErpWarehousingDetailsVo erpWarehousingDetailsVo : erpWarehousingVo.getDetailsList()) {
            WarehousingExcelExport warehousingExcelExport = new WarehousingExcelExport();
            warehousingExcelExport.setWarehousingNo(erpWarehousingVo.getWarehousingNo());
            warehousingExcelExport.setProcureOdd(erpWarehousingVo.getProcureOdd());
            warehousingExcelExport.setSupplierOrCustomerName(erpWarehousingVo.getSupplierOrCustomerName());
            warehousingExcelExport.setWarehousingType(erpWarehousingVo.getWarehousingType());
            warehousingExcelExport.setWareName(erpWarehousingVo.getWareName());
            warehousingExcelExport.setAnswerScheduledReceipt(erpWarehousingVo.getAnswerScheduledReceipt());
            warehousingExcelExport.setNickName(erpWarehousingVo.getNickName());
            warehousingExcelExport.setPhonenumber(erpWarehousingVo.getPhonenumber());
            warehousingExcelExport.setWarehousingTime(erpWarehousingVo.getWarehousingTime());
            warehousingExcelExport.setWarehousingStatus(erpWarehousingVo.getWarehousingStatus());
            warehousingExcelExport.setRemark(erpWarehousingVo.getRemark());
            warehousingExcelExport.setSupplierCommodityNo(erpWarehousingDetailsVo.getSupplierCommodityNo());
            warehousingExcelExport.setSupplierCommodityName(erpWarehousingDetailsVo.getSupplierCommodityName());
            warehousingExcelExport.setCommodityType(erpWarehousingDetailsVo.getCommodityType());
            warehousingExcelExport.setSupplierCommoditySpec(erpWarehousingDetailsVo.getSupplierCommoditySpec());
            warehousingExcelExport.setSupplierCommodityUnit(erpWarehousingDetailsVo.getSupplierCommodityUnit());
            warehousingExcelExport.setUnitRelationship(erpWarehousingDetailsVo.getUnitRelationship());
            warehousingExcelExport.setProcureNumber(erpWarehousingDetailsVo.getProcureNumber());
            warehousingExcelExport.setRealityNumber(erpWarehousingDetailsVo.getRealityNumber());
            list.add(warehousingExcelExport);
        }
        ExcelUtil<WarehousingExcelExport> util = new ExcelUtil<>(WarehousingExcelExport.class);
        util.exportExcel(response, list, "采购入库数据");
    }

    /**
     * 获取采购入库详细信息
     */
    @RequiresPermissions("erp:ErpWarehousing:query")
    @GetMapping(value = "/{warehousingId}")
    public AjaxResult getInfo(@PathVariable("warehousingId") Long warehousingId) {
        return success(erpWarehousingService.selectErpWarehousingByWarehousingId(warehousingId));
    }

    /**
     * 新增采购入库
     */
    @RequiresPermissions("erp:ErpWarehousing:add")
    @Log(title = "采购入库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ErpWarehousing erpWarehousing) {
        return toAjax(erpWarehousingService.insertErpWarehousing(erpWarehousing));
    }

    /**
     * 修改采购入库
     */
    @RequiresPermissions("erp:ErpWarehousing:edit")
    @Log(title = "采购入库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ErpWarehousingDto erpWarehousing) {
        return toAjax(erpWarehousingService.updateErpWarehousing(erpWarehousing));
    }

    /**
     * 删除采购入库
     */
    @RequiresPermissions("erp:ErpWarehousing:remove")
    @Log(title = "采购入库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{warehousingIds}")
    public AjaxResult remove(@PathVariable Long[] warehousingIds) {
        return toAjax(erpWarehousingService.deleteErpWarehousingByWarehousingIds(warehousingIds));
    }
}
